﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bizu.Models;
using Newtonsoft.Json;

namespace Bizu.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        BizuEntities db = new BizuEntities();

        public ActionResult Index()
        {
            return View();
        }


        //Get categories từ data
        public string getCateName() 
        {
            var q = (from c in db.bizu_Categories select c).ToList();

            return JsonConvert.SerializeObject(q);
        }

        //thêm bài viết mới
        [HttpPost]
        [ValidateInput(false)]
        public string insertNewPost(bizu_Post newPost)
        {
            string titlePost = Request.Form["title"];
            string cateName = Request.Form["cateNamePost"];
            string contentPost = Request.Form["contentPost"];

            int cateID =  (from c in db.bizu_Categories where c.name_cate.Equals(cateName) select c.id).FirstOrDefault();
            newPost.cate_id = cateID;
            newPost.title = titlePost;
            newPost.content_post = modul.modulPost.convertPostContent(contentPost);
            newPost.date_post = Convert.ToDateTime(DateTime.Now.ToLongTimeString());
            newPost.count_view = 0;
            newPost.count_comment = 0;
            newPost.images_post = "";
            newPost.user_id = 1;

            db.bizu_Post.Add(newPost);
            db.SaveChanges();

            return newPost.id.ToString();
        }

        //Show bài viết cho mục quản lý bài viết
        public string getListPost() 
        {
            var q = (from p in db.bizu_Post
                     select new
                     {
                        idPost = p.id,
                        titlePost = p.title,
                        contentPost = p.content_post.Substring(0,80),
                        datePost = p.date_post,
                     }).ToList();

            return JsonConvert.SerializeObject(q);
        }

        //delete Post by ID

        public void deletePostByID(int idPost = 0)
        {
            var q = (from p in db.bizu_Post where p.id == idPost select p).FirstOrDefault();
            db.bizu_Post.Remove(q);
            db.SaveChanges();
        }

        //get all comment
        public string getAllComment() 
        {
            var q = (from c in db.bizu_Comment
                     select new
                     {
                         idComment = c.id,
                         content_comment = c.content_comment,
                         name_post = (from p in db.bizu_Post where c.post_id == p.id select p.title).FirstOrDefault(),
                         user_name = "Trần Lê Cường",
                     }
                      ).ToList();

            return JsonConvert.SerializeObject(q);
        }
    }
}
